const {createClockIn,findClockIn,updateClockIn,findClockInById} = require("../controller/clockInController")
const {findOneMoveAboutById} = require("../controller/moveAboutController")
const {ValidationError} = require("../utils/customError");
// 创建打卡记录
exports.addClockIn = async (clockInfo,userId)=>{
    if(!clockInfo.recruitmentInfo){
       throw new ValidationError("请传入招募ID")
    }
    const result = await findClockInById(clockInfo.recruitmentInfo,userId);
    if(result  && result.workingHours === 0){
        throw new ValidationError("还有未签退的任务");
    }
    const recruitInfo = await findOneMoveAboutById(clockInfo.recruitmentInfo)
    if(!recruitInfo.isClockIn){
        throw new ValidationError("企业暂未开启打卡")
    }
    return createClockIn(clockInfo)
}


exports.editClockIn = async (id,clockInfo,userId)=>{
    const result = await findClockInById(id,userId);
    clockInfo.signOutTime = new Date().getTime()
    clockInfo.workingHours = clockInfo.signOutTime - result.signInTime;
    return updateClockIn(id,clockInfo)
}

exports.getClockIn = (query)=>{
    return findClockIn(query)
}
